﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace On_the_Fly
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        bool currentlyOpen = true, mD = false;
        Point point = new Point(0, 0);

        private void Form1_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.FirstTimeUser)
            {
                ShowSettings();
            }

            // Prevent Windows from auto-fucking with my size.
            this.Size = new Size(268, 35);

            // Set the initial/default location
            this.Location = new Point(
                Screen.PrimaryScreen.WorkingArea.Right - this.Width,
                Screen.PrimaryScreen.WorkingArea.Top + 400);

            // Focus, so as soon as user clicks away, it'll auto-hide.
            this.Focus();
        }

        void OpenOrClose()
        {
            if (currentlyOpen)
            {
                this.Location = new Point(
                    Screen.PrimaryScreen.WorkingArea.Right - 15,
                    Screen.PrimaryScreen.WorkingArea.Top + 400);

                currentlyOpen = false;
            }
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            OpenOrClose();
            currentlyOpen = false;
        }

        private void logo_Click(object sender, EventArgs e)
        {
            Process.Start("http://onthefly.codeplex.com/");
        }

        private void logo_MouseEnter(object sender, EventArgs e)
        {
            this.Location = new Point(
                Screen.PrimaryScreen.WorkingArea.Right - 35,
                Screen.PrimaryScreen.WorkingArea.Top + 400);

            this.Activate(); // Enables tooltips to appear even if user doesn't click to activate form.
            currentlyOpen = true;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            point = new Point(e.X, e.Y);
            if (!mD)
                mD = true;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            mD = false;

            // Auto place based on location.
            if (this.Location.X > Screen.PrimaryScreen.WorkingArea.Right - 125)
            { OpenOrClose(); }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mD)
            {
                if (this.Location.X > Screen.PrimaryScreen.WorkingArea.Right - this.Width)
                {
                    this.Location = new Point(
                        this.Location.X + e.X - point.X, this.Location.Y);
                }
            }

            this.Refresh();
        }

        private void panel2_MouseEnter(object sender, EventArgs e)
        {
            settingsContainer.BackColor = Color.FromArgb(0, 124, 225);
        }

        private void panel2_MouseLeave(object sender, EventArgs e)
        {
            settingsContainer.BackColor = Color.FromArgb(0, 133, 221);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowSettings();
        }

        void ShowSettings()
        {
            using (Settings settings = new Settings())
            {
                settings.ShowDialog();
            }
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        private void compiler_Click(object sender, EventArgs e)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(Properties.Settings.Default.WorkingDirectory);
                FileInfo[] files = dir.GetFiles("*.ts");

                foreach (FileInfo file in files)
                {
                    try
                    {
                        Process.Start(Properties.Settings.Default.CompilerPath + @"\tsc.exe", "\"" + file.FullName + "\"");
                        status.Text = "Compiling: " + file.FullName;
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                        status.Text = "Compilation failure.";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
